package com.iscas.green.extralindextest.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class PostgreSQLConfig {
    
    @Value("${postgresql.datasource.jdbc-url}")
    private String jdbcUrl;
    
    @Value("${postgresql.datasource.username}")
    private String username;
    
    @Value("${postgresql.datasource.password}")
    private String password;
    
    @Value("${postgresql.datasource.driver-class-name}")
    private String driverClassName;
    
    @Bean(name = "postgresqlDataSource")
    public DataSource postgresqlDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(jdbcUrl);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setDriverClassName(driverClassName);
        // 可以在这里设置其他HikariCP特有的配置
        dataSource.setMaximumPoolSize(10);
        dataSource.setMinimumIdle(2);
        dataSource.setIdleTimeout(30000);
        dataSource.setMaxLifetime(1800000);
        dataSource.setConnectionTimeout(20000);
        return dataSource;
    }
    
    @Bean(name = "postgresqlJdbcTemplate")
    public JdbcTemplate postgresqlJdbcTemplate(@Qualifier("postgresqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
} 